<?php

// 广告位表

namespace app\console\model;
use think\Db;
use think\Model;
use think\Request;
use app\common\trilateral\Classify as ClassifyAlias;
use think\Session;
use think\Hook;

class Gift extends Model
{
    public function ruku($id){
        $param = Request::instance()->param();

        if(Request::instance()->isPost()){
            if($param['count'] <= 0){
                fail('添加数量不能为0');
            }
            $role_id = session('role_id');

            if($role_id == 2){
                $role_id = 11;  //如果是超级管理员或者管理员就当一级审核原来玩

            }
            $auding_text = '等待审核';
            if($role_id == 11){
                $auding_text = '一级审核';
            }
            if($role_id == 12){
                $auding_text = '二级审核';
            }
            if($role_id == 13){
                $auding_text = '三级审核';
            }
            $manage_id = session('manage_id');
            if (isset($param['auding_status'])) { //有审核状态 审核权限人 2是审核成功
                //多次审核
                if($role_id != 1){
                    $ssaas_find = Db::name('shop_spec_attr_auding_status')->where('type',1)->where('spec_id',$param['id'])->where('role_id',3)->find();
                    if(empty($ssaas_find)){
                        fail('没有录入员提交,无法审核');
                    }
                }
                if($role_id == 12){
                    $auding_status1 = Db::name('shop_spec_attr_auding_status')->where('type',1)->where('spec_id',$param['id'])->where('role_id',11)->find();
                    if(empty($auding_status1)){
                        fail('请等一级审核完毕');
                    }
                }
                if($role_id == 13){
                    $auding_status1 = Db::name('shop_spec_attr_auding_status')->where('type',1)->where('spec_id',$param['id'])->where('role_id',11)->find();
                    $auding_status2 = Db::name('shop_spec_attr_auding_status')->where('type',1)->where('spec_id',$param['id'])->where('role_id',11)->find();
                    if(empty($auding_status1) || empty($auding_status2)){
                        fail('请等一二级审核完毕');
                    }
                }

                //超级管理员1 管理员2 一级审核员11 二级审核员12 三级审核员13
                $status = false;

                if($param['auding_status'] != 2){ //不是成功就是删除 重新来
                    Db::name('shop_spec_attr_auding_status')->where('spec_id',$param['id'])->delete(); //全部删除重新审核
                    Db::name('shop_spec_attr')->where('id',$param['id'])->setField('reason',$param['reason']);
                    fail('您不同意审核,审核重新开始');
                }else{ //=2
                    //成功
                    $find = Db::name('shop_spec_attr_auding_status')->where([
                        'type' => 1,
                        'spec_id' => $param['id'],
                        'auding_status' => $param['auding_status'],
                        'role_id' => $role_id
                    ])->find(); //当前会员

                    if(!empty($find)) fail('本级别已经审核过');
                    $select = Db::name('shop_spec_attr_auding_status')->where([
                        'type' => 1,
                        'spec_id' => $param['id'],
                    ])->select(); //统计数量
                    if($role_id == 1){
                        $select = ['1','2',3];
                    }
                    if(count($select) == 3){ //如果已经有2条同意了 那就是正常添加了
                        Db::name('shop_spec_attr_auding_status')->where([
                            'type' => 1,
                            'spec_id' => $param['id'],
                        ])->delete(); //删除统计记录
                        Db::name('shop_spec_attr_bit')->insert([
                            'spec_attr_id' => $param['id'],
                            'goods_id' => $param['goods_id'],
                            'bit' => $param['bit'],
                            'count' => $param['count'],
                            'total_bit' => $param['bit']*$param['count'],
                            'attribute' => $param['attribute'],
                            'inventory' => $param['inventory'],
                            'after_inventory' => $param['inventory']+$param['count'],
                            'create_time' => date('Y-m-d H:i:s',time())
                        ]);
                        $goods_attr = Db::name('shop_spec_attr')->where('id',$id)->setInc('inventory',$param['count']);
                        Db::name('shop_spec_attr')->where('id',$id)->update(['count' =>$param['count'],'bit' => $param['bit'],'total_bit' => $param['count']*$param['bit']]);
                        if($goods_attr){
                            $this->insert_records(1,$param['count'],$id);
                            win('入库成功',Url::build('chuku'));
                        }
                    }
                    Db::name('shop_spec_attr_auding_status')->insert([
                        'auding_text' => $auding_text,
                        'count' => $param['count'],
                        'bit' => $param['bit'],
                        'type' => 1,
                        'spec_id' => $param['id'],
                        'auding_status' => $param['auding_status'],
                        'auding_id' => $manage_id,
                        'role_id' => $role_id,
                        'create_time' => date('Y-m-d H:i:s',time())

                    ]);


                    Db::name('shop_spec_attr')->where('id',$param['id'])->setField('reason',''); //成功 就没有失败原因 清空
                    win('审核完毕',Url::build('chuku'));
                }
//                if($role_id == 1 || $role_id == 2 || $role_id == 11){
//                    Db::name('shop_spec_attr_auding_status')->insert();
//                    if($auding_status1 == 2 && $auding_status2 == 2 && $auding_status3 ==2){ //这有这一种情况是审核通过 其他全部是失败
//                        $status = true;
//                    }
//
//                }
//                if($role_id == 12){ //2级审核
//                    if($auding_status1 == 2 && $auding_status1 == 2 && $auding_status3 ==2){ //这有这一种情况是审核通过 其他全部是失败
//                        $status = true;
//                    }
//                }
//                if($role_id == 13){
//                    if($auding_status1 == 2 && $auding_status1 == 2 && $auding_status2 ==2){ //这有这一种情况是审核通过 其他全部是失败
//
//                        $status = true;
//                    }
//                }
//                if($status == true){
//                    $data->auding_status = 2;
//                    Db::name('member_coupon')->where('coupon_id',$data->id)->setField('auding_status',2);
//                    foreach (explode(',',Db::name('coupon')->where('id',$data->id)->value('shop_assignor_id')) as $k => $v){
//                        (new \app\index\controller\MemberMessage())->jpushMessage($v,'恭喜您获得优惠券'.Db::name('coupon')->where('id',$data->id)->value('number').'张',Db::name('coupon')->where('id',$data->id)->value('title'),3);
//
//                    }
//
//                }else{
//                    // $data->auding_status = $data->auding_status;使用默认提交值
//                    $data->auding_status = 3; //必须失败 否则 默认提交过来的auding_status 自动还是变成 1 2 3 状态;
//                    Db::name('member_coupon')->where('coupon_id',$data->id)->setField('auding_status',1);
//                }

            }else{
                $find = Db::name('shop_spec_attr_auding_status')->where([
                    'type' => 1,
                    'spec_id' => $param['id'],
                    'auding_status' => 2,
                    'role_id' => $role_id
                ])->find(); //当前会员

                if(!empty($find)) fail('本级别已经审核过');
                Db::name('shop_spec_attr_auding_status')->insert([
                    'auding_text' => $auding_text,
                    'count' => $param['count'],
                    'bit' => $param['bit'],
                    'type' => 1,
                    'spec_id' => $param['id'],
                    'auding_status' => 2,
                    'auding_id' => $manage_id,
                    'role_id' => $role_id,
                    'create_time' => date('Y-m-d H:i:s',time())

                ]);
                win('录入完毕',Url::build('chuku'));
            }
//            Db::name('shop_spec_attr_bit')->insert([
//                'spec_attr_id' => $param['id'],
//                'goods_id' => $param['goods_id'],
//                'bit' => $param['bit'],
//                'count' => $param['count'],
//                'total_bit' => $param['bit']*$param['count'],
//                'attribute' => $param['attribute'],
//                'inventory' => $param['inventory'],
//                'after_inventory' => $param['inventory']+$param['count'],
//                'create_time' => date('Y-m-d H:i:s',time())
//            ]);
//            $goods_attr = Db::name('shop_spec_attr')->where('id',$id)->setInc('inventory',$param['count']);
//            Db::name('shop_spec_attr')->where('id',$id)->update(['count' =>$param['count'],'bit' => $param['bit'],'total_bit' => $param['count']*$param['bit']]);
//            if($goods_attr){
//                $this->insert_records(1,$param['count'],$id);
//
//                win('入库成功',Url::build('chuku'));
//            }

        }
        $shenhe = Db::name('shop_spec_attr_auding_status')
            ->alias('s')
            ->join('manager m','s.auding_id = m.id')
            ->field('m.*,s.*')
            ->where(['spec_id' => $param['id'],'type' => 1])
            ->find();
        $records = Db::name('shop_spec_attr_auding_status')
            ->alias('s')
            ->join('manager m','s.auding_id = m.id','left')
            ->field('m.*,s.*')
            ->where(['spec_id' => $param['id'],'type' => 1])
            ->select()->toArray();
        foreach ($records as $k => &$v){
            $v['level'] = '等待审核';
            if($v['role_id'] == 11){
                $v['level'] = '一级审核';
            }
            if($v['role_id'] == 12){
                $v['level'] = '二级审核';
            }
            if($v['role_id'] == 13){
                $v['level'] = '三级审核';
            }


        }

        return view('',['item' => $shenhe,'records' => $records]);
    }
    // 模型事件
    protected static function init()
    {

        self::event('before_write', function ($data) {
            if($data->sycss < 0){
                //fail('添加次数必须大于0');
            }

//            if(\request()->action() == 'create'){
//                $data->zcs = $data->sycs;
//            }else{
//
//            }
            if(isset($data->interval_timer)){
                $t = explode('/',$data->interval_timer);
                $data->start_time = $t[0];
                $data->end_time = $t[1];
            }

            $data->single_pic = uploadImg('single_pic');
            $data->multi_pic = serialize(Request::instance()->param('picArr/a'));// 组图
//            if(\request()->action() == 'update'){
//                $now_data = Request::instance()->param();
//                unset($now_data['interval_timer']);
//                unset($now_data['content']);
//                unset($now_data['buy_must_know']);
//                unset($now_data['member_id']);
//                unset($now_data['start_time']);
//                unset($now_data['end_time']);
//                unset($now_data['auding_status']);
//                unset($now_data['auding_status1']);
//                unset($now_data['auding_status2']);
//                unset($now_data['auding_status3']);//zcs
//                $auding = true;
//                foreach ($now_data as $k => $v){
//                    if($v != Db::name('gift')->where('id',$data['id'])->value($k)){
//                        //Db::name('gift')->where('id',$data['id'])->update(['auding_status' => 1]);
//                        //dump($k);
//                        $auding = false;
//                        $data->auding_status = 1;
//                        //$data->status1 = 1;
//                        //$data->status2 = 1;
//                        // $data->status3 = 1;
//                    }
//                }
//
//            }
            audinggift2($data);
            //auding($data);



        });
        self::event('after_write', function ($data) {
            $param = Request::instance()->param();

            if(!empty($param['single_id'])){
            Db::name('gift_count')->where('gift_id',$data->id)->delete();
            }
            $insertAll = [];
            if(!empty($param['single_id'])){ //服务类id
                for ($i = 0; $i < count($param['single_id']); $i++) {
                    if($param['gift_count'][$i] >= 10000 || $param['day'][$i] >= 10000){
                        fail('请填写最大4位次数和天数');
                    }
                    if($param['gift_count'][$i] <= 0 || $param['day'][$i] <= 0){
                        fail('请填写大于0的次数和天数');
                    }
                    $insertAll[$i]['gift_id'] = $data->id;
                    $insertAll[$i]['single_content'] = $param['single_content'][$i];
                    $insertAll[$i]['single_id'] = $param['single_id'][$i];
                    $insertAll[$i]['gift_count'] = $param['gift_count'][$i];
                    $insertAll[$i]['day'] = $param['day'][$i];
                }
                (new GiftCount())->saveAll($insertAll);
            }

        });
    }

    protected function getDcTextAttr($value, $data)
    {

        $status = [1 => '借方', 2 => '贷方'];
        return $status[$data['dc']];
    }

    protected function base($query)
    {
        $query->where('is_delete',1);
    }

    public function getAudingTextAttr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status']];
    }
    public function getCountAttr($value, $data)
    {
        $ret = Db::name('gift_count')->where('gift_id',$data['id'])->sum('gift_count');
        return $ret;
    }

    public function getAudingText1Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status1']];
    }
    public function getAudingText2Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status2']];
    }
    public function getAudingText3Attr($value, $data)
    {
        $status = [1 => '等待审核', 2 => '审核成功', 3 => '审核失败'];
        return $status[$data['auding_status3']];
    }
//
    public function getAudingManageAttr($value, $data)
    {
       $ret = Db::name('manager')->where('id',$data['auding_id'])->value('username');
        return $ret;
    }
    public function getCreateManageAttr($value, $data)
    {
        $ret = Db::name('manager')->where('id',$data['create_id'])->value('username');
        return $ret;
    }
//    public function getNewsTypeAttr($value, $data)
//    {
//        $ret = Db::name('news_type')->where('id',$data['type_id'])->value('type_name');
//        return $ret;
//    }

}